#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 15 20:15:29 2021

@author: root
"""

# 检测人脸
import face_recognition
import cv2
 
# 读取图片并识别人脸
img = face_recognition.load_image_file("Image.png")#导入图片
face_locations = face_recognition.face_locations(img)#识别人脸
#face_recognition.face_locations 的参数是矩形 [(x1,y1,x2,y2)] 矩形左上角右下角
print (face_locations)#打印人脸位置
 
# 调用opencv函数显示图片
img = cv2.imread("Image.png")#读入原图
cv2.namedWindow("原图")# 窗口名为原图
cv2.imshow("原图", img)#展示图片
# 遍历每个人脸，并标注
faceNum = len(face_locations)
for i in range(0, faceNum):
    top =  face_locations[i][0]
    right =  face_locations[i][1]
    bottom = face_locations[i][2]
    left = face_locations[i][3]
 
    start = (left, top)
    end = (right, bottom)
    color = (55,255,155)#颜色 绿色
    thickness = 3 #粗细
    cv2.rectangle(img, start, end, color, thickness)#绘制矩形
 
# 显示识别结果
cv2.namedWindow("识别")
cv2.imshow("识别", img)
 
cv2.waitKey(0)
cv2.destroyAllWindows()
